# encoding: utf-8
# author: 侯佳涛
# file_name: sms_verify
# create_time: 2022/2/2, 下午12:10
from ronglian_sms_sdk import SmsSDK

accId = '8a216da873222571017322a8add80058'
accToken = '54ca76ae2fae417ea1616a8cbb433467'
appId = '8aaf0708732220a6017322fbc7dd00fa'


# def send_message(tid, mobile, datas):
#     """
#     :param tid: 模版编号
#     :param mobile: '手机号1,手机号2,...' 最多一次200个手机号
#     :param datas: ('变量1', '变量2')
#     :return:
#     """
#     sdk = SmsSDK(accId, accToken, appId)
#     resp = sdk.sendMessage(tid, mobile, datas)
#     print(resp)


class CCP:
    """
        发送短信验证码的单例类
        特点：只在内存中开辟一个该类的实力对象，节约内存空间
    """
    def __new__(cls):
        """初始化单例"""
        # 判断该类在内存中是否已被初始化
        if not hasattr(cls, '_instance'):  # 第二个参数是自定义的名字，判断内存中该类是否已经被初始化
            # 没有就创建该类的单例对象
            cls._instance = super(CCP, cls).__new__(cls)
            # 初始化发送短信SDK对象
            cls._instance.sdk = SmsSDK(accId, accToken, appId)

        # 返回对象
        return cls._instance

    def send_sms(self, tid, mobile, datas):
        """
        单例类的方法要写成对象方法，不能写成类方法
        :param tid: 模版编号
        :param mobile: '手机号1,手机号2,...' 最多一次200个手机号
        :param datas: ('变量1', '变量2')
        :return: 成功:0  失败:-1
        """
        result = self.sdk.sendMessage(tid, mobile, datas)
        print(result)
        # print(type(result))

        if eval(result).get('statusCode') == '000000':
            # 发送成功
            return 0
        else:  # 发送失败
            return -1


if __name__ == '__main__':
    # send_message(1, '13261690563', ('123456', '5'))
    ccp = CCP()
    ccp.send_sms(1, '13261690563', ('123456', '5'))

